package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WarehouseAreaMapper;
import com.ruoyi.wms.domain.WarehouseArea;
import com.ruoyi.wms.service.IWarehouseAreaService;

/**
 * 库区库位Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-20
 */
@Service
public class WarehouseAreaServiceImpl implements IWarehouseAreaService {
    @Autowired
    private WarehouseAreaMapper warehouseAreaMapper;

    /**
     * 查询库区库位
     *
     * @param id 库区库位主键
     * @return 库区库位
     */
    @Override
    public WarehouseArea selectWarehouseAreaById(String id) {
        return warehouseAreaMapper.selectWarehouseAreaById(id);
    }

    /**
     * 查询库区库位列表
     *
     * @param warehouseArea 库区库位
     * @return 库区库位
     */
    @Override
    public List<WarehouseArea> selectWarehouseAreaList(WarehouseArea warehouseArea) {
        return warehouseAreaMapper.selectWarehouseAreaList(warehouseArea);
    }

    /**
     * 新增库区库位
     *
     * @param warehouseArea 库区库位
     * @return 结果
     */
    @Override
    public int insertWarehouseArea(WarehouseArea warehouseArea) {
        warehouseArea.setId(MD5Utils.md5(warehouseArea.getAreaCode()));
        warehouseArea.setCreateUser(SecurityUtils.getUsername());
        warehouseArea.setCreateDate(DateUtils.getCurrentTime());
        warehouseArea.setUpdateDate( DateUtils.getCurrentTime());
        return warehouseAreaMapper.insertWarehouseArea(warehouseArea);
    }

    /**
     * 修改库区库位
     *
     * @param warehouseArea 库区库位
     * @return 结果
     */
    @Override
    public int updateWarehouseArea(WarehouseArea warehouseArea) {

        int updateResult = 0;
        if (StringUtils.isEmpty(warehouseArea.getId())) {
            updateResult = insertWarehouseArea(warehouseArea);
        }
        warehouseArea.setUpdateDate( DateUtils.getCurrentTime());
        updateResult = warehouseAreaMapper.updateWarehouseArea(warehouseArea);
        if (0 == updateResult) {
            updateResult = insertWarehouseArea(warehouseArea);
        }

        return updateResult;


    }

    /**
     * 批量删除库区库位
     *
     * @param ids 需要删除的库区库位主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseAreaByIds(String[] ids) {
        return warehouseAreaMapper.deleteWarehouseAreaByIds(ids);
    }

    /**
     * 删除库区库位信息
     *
     * @param id 库区库位主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseAreaById(String id) {
        return warehouseAreaMapper.deleteWarehouseAreaById(id);
    }
}
